package com.lancoo.klgcourseware.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.lancoo.klgcourseware.utils.KlgToolUtils;

/**
 * Created by pengmi on 2019/12/31 14:47
 * Description:
 */
public class CountTimeView extends View {

    private Paint paint0,paint1,paint2;
    private int strockWidth=4;
    private int angle;

    public CountTimeView(Context context) {
        super(context);
    }

    public CountTimeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint(context);
    }

    public CountTimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint(context);
    }


    private void initPaint(Context context) {
        strockWidth=KlgToolUtils.dip2px(context,2);
        int screenWidth = KlgToolUtils.getWidthInPx(context);
        int screenHeight = KlgToolUtils.getHeightInPx(context);
        paint0 = new Paint();
        paint0.setColor(Color.parseColor("#ff04c3fa"));
        paint0.setStyle(Paint.Style.FILL);
        paint0.setAntiAlias(true);
        paint1 = new Paint();
        paint1.setColor(Color.parseColor("#ff04c3fa"));
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setAntiAlias(true);
        paint1.setStrokeWidth(strockWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width=getWidth();
        int height=getHeight();
        int radius=width/2-strockWidth;
        int circleX=width/2;
        int circleY=height-radius-strockWidth;
        float smallRadius=radius-strockWidth*2f;
        float smallSubRadius=radius-strockWidth*4f;
        canvas.translate(circleX, circleY);
        //画圆环
        canvas.drawCircle(0, 0, smallRadius, paint1);
//        canvas.drawCircle(circleX, circleY, radius-strockWidth*1.5f, paint0);
        RectF rectF=new RectF();
        rectF.set(-smallSubRadius,-smallSubRadius,smallSubRadius,smallSubRadius);
        RectF rectF1=new RectF();
        rectF1.set(-radius,-radius,radius,radius);
        //画扇形
        canvas.drawArc(rectF,270,angle,true,paint0);
        //画弧线
//        canvas.drawArc(rectF1,300,angle*300/360,false,paint1);
        canvas.drawArc(rectF1,300,18,false,paint1);
        int left=-radius*2/5;
        int right=+radius*2/5;
        int top=-radius-strockWidth*4;
        int bottom=-radius-(int)(strockWidth*2f);
        Rect rect=new Rect(left,top,right,bottom);
        //画大矩形
        canvas.drawRect(rect,paint0);
        //画小矩形
        Rect smallRect=new Rect(left*6/10,bottom,right*6/10,bottom+(int)(strockWidth*2f));
        canvas.drawRect(smallRect,paint0);
    }

    public void setAngle(int angle,int totalAngle){
        if(angle>=0&&totalAngle!=0){
            this.angle=angle*360/totalAngle;
        }else{
            this.angle=0;
        }
        invalidate();
    }
}
